//分钟倒计时
function secTimer(config) {
    var _settings = {
        obj : null,
        beginMsg : '获取验证码',
        msg : "倒计时",
        totalTime : 5000,
        spaceTime : 1000,
        className : "",
        callBack : null,
        init : function(config){
            for(var key in config){
                this[key] = config[key];
                console.log("init-->",key,config[key]);
            }
            this.check();
        },
        check : function(){
            if (this.obj === null) {
                console.log("obj is null");
                return;
            };
            if (this.totalTime <= 0) {
                console.log("totalTime is error");
                return;
            };
            if (this.spaceTime <= 0) {
                console.log("spaceTime is error");
                return;
            };
            this.run();
        },
        run : function(){
            var that = _settings;
            if (that.totalTime > 0) {
            //开始倒计时
                that.obj.innerHTML = that.totalTime / 1000 + that.msg;
                that.totalTime = that.totalTime - that.spaceTime;
                //输出时间剩余时间
                console.log(that.totalTime);
                document.cookie = 'secTimer=' + that.totalTime;
                that.obj.disabled = 'disabled';
                that._addClass(that.obj,that.className);
                setTimeout(that.run,that.spaceTime);
            }else{
                //时间结束
                console.log('time over');
                that.delCookie('secTimer');
                that.obj.innerHTML = that.beginMsg;
                that.obj.disabled = '';
                that._removeClass(that.obj,that.className);
                if (that.callBack != null) {
                    that.callBack();
                }
            }
        },
        getCookie : function (name) {
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg))

                return unescape(arr[2]); 
            else
                return null;
        },
        delCookie : function (name) {
            var date = new Date();
            date.setTime(date.getTime() - 1);
            var val = this.getCookie(name);
            if (val != null) {
                document.cookie = name + '=' + val + ';expires = ' + date.toGMTString;
            }
        },
        _hasClass : function(obj, cls) {
            return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
        },
        _addClass : function(obj, cls) {
            if (!this._hasClass(obj, cls))
                obj.className += " " + cls;
        },
        _removeClass : function(obj, cls) {
            if (this._hasClass(obj, cls)) {
                var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
                obj.className = obj.className.replace(reg, ' ');
            }
        },
        _toggleClass : function(obj, cls) {
            if (this._hasClass(obj, cls)) {
                this._removeClass(obj, cls);
            } else {
                this._addClass(obj, cls);
            }
        }
    };
    _settings.init(config);
}